package progress.exa1;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

public class FileStudy {
    public static void main(String[] args) throws IOException {

        //existsStudy();
        //getNameStudy();
//        canReadStudy();
//        canWriteStudy();
//        lengthStudy();
//        absolutePathStudy();
//        parentStudy();
//        isFileStudy();
//        isDirectory();

//        isHidden();
        //lastModifiedStudy();
//        mkdirStudy();
        //listStudy();
        listFilesStudy();
        //createNewFileStudy();
       // deleteStudy();
    }

    public static void fileStudy(){
        File file  = new File("麻子.txt");
        File file1 = new File("E:/Personal/MyRepository/study","麻子.txt");
        File dir = new File("E:/Personal/MyRepository/study");
        File file2 = new File(dir,"麻子.txt");
        System.out.println("file 文件路径："+file.getAbsolutePath());
        System.out.println("file1 文件路径： "+file1.getAbsolutePath());
        System.out.println("dir 文件路径： "+dir.getAbsolutePath());
        System.out.println("file2 文件路径： "+file2.getAbsolutePath());
    }

    public static void existsStudy(){
        File file = new File("麻子.txt");
        System.out.println("麻子.txt 文件是否存在： "+file.exists());
        File file1 = new File("四郎.txt");
        System.out.println("四郎.txt 是否存在： "+file1.exists());
    }

    public static void getNameStudy(){
        File file = new File("麻子.txt");
        System.out.println("麻子.txt文件名称： "+file.getName());
        File file1 = new File("四郎.txt");
        System.out.println("四郎.txt 文件名称： "+file1.getName());
    }

    public static void canReadStudy() {
        File file = new File("麻子.txt");
        System.out.println("麻子.txt文件是否可读： "+file.canRead());
        File file1 = new File("四郎.txt");
        System.out.println("四郎.txt是否可读： "+file1.canRead());
    }

    public static void canWriteStudy() {
        File file = new File("麻子.txt");
        System.out.println("麻子.txt是否可写： "+file.canWrite());
        File file1 = new File("四郎.txt");
        System.out.println("四郎.txt是否可写： "+file1.canWrite());
    }

    public static void lengthStudy() {
        File file = new File("麻子.txt");
        System.out.println("麻子.txt文件大小： "+file.length());
        File file1 = new File("四郎.txt");
        System.out.println("四郎.txt文件大小： "+file1.length());
    }

    public static void absolutePathStudy() {
        File file = new File("麻子.txt");
        System.out.println("麻子.txt 文件绝对路径： "+file.getAbsolutePath());
        File file1 = new File("四郎.txt");
        System.out.println("四郎.txt 文件绝对路径： "+file1.getAbsolutePath());
    }

    public static void parentStudy(){
        File file = new File("麻子.txt");
        System.out.println("麻子.txt 文件父目录： "+file.getParent());
        File file1 = new File("四郎.txt");
        System.out.println("四郎.txt 文件父目录： "+file1.getParent());
    }

    public static void isFileStudy(){
        File file = new File("麻子.txt");
        System.out.println("麻子.txt 是否是文件： "+file.isFile());
        File file1 = new File("四郎.txt");
        System.out.println("四郎.txt 是否是文件： "+file1.isFile());
    }

    public static void isDirectory(){
        File file = new File("麻子.txt");
        System.out.println("麻子.txt是否是目录: "+file.isDirectory());
        File file1 = new File("四郎.txt");
        System.out.println("四郎.txt是否是目录： "+file1.isDirectory());
    }

    public static void isHidden(){
        File file = new File("麻子.txt");
        System.out.println("麻子.txt是否是隐藏文件： "+file.isHidden());
        File file1 = new File("四郎.txt");
        System.out.println("四郎.txt 是否是隐藏文件： "+file1.isHidden());
    }

    public static void lastModifiedStudy(){
        File file = new File("麻子.txt");
        System.out.println("麻子.txt最后修改时间： "+file.lastModified());
        File file1 = new File("四郎.txt");
        System.out.println("四郎.txt 件最后修改时间： "+file1.lastModified());
    }

    public static void mkdirStudy(){
        File file1 = new File("FileStudy");
        if(!file1.exists()){
            boolean sus = file1.mkdir();
            if(sus){
                System.out.println("FileStudy 目录创建成功");
            }
        }
    }

    public static void listStudy(){
        File file = new File("Java基础学习");
        String[] dir = file.list();
        assert dir != null;
        for(String d:dir){
            System.out.println("Java基础学习子目录： "+d);
        }

        String[] dir1 = file.list((dir2, name) -> name.contains("tar"));

        assert dir1 != null;
        for(String d2:dir1){
            System.out.println("Java基础学习子目录中名称包含 tar 的目录："+d2);
        }
    }

    public static void listFilesStudy(){
        File file = new File("Java基础学习");
        File[] files = file.listFiles();
        assert files != null;
        for(File file1:files){
            System.out.println("Java基础学习子目录： "+file1.getName());
        }
        File[] dir1 = file.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                if(name.contains("tar"))
                    return true;
                return false;
            }
        });
        assert dir1 != null;
        for(File d2:dir1){
            System.out.println("Java基础学习子目录中名称包含 tar 的目录："+d2.getName());
        }
    }

    public static void createNewFileStudy() throws IOException {
        File file = new File("四郎.txt");
        if(!file.exists()){
            boolean sus = file.createNewFile();
            if(sus) System.out.println("四郎.txt 文件创建完成！！");
        }
    }

    public static void deleteStudy(){
        File file = new File("四郎.txt");
        boolean sus = file.delete();
        if(sus) System.out.println("四郎.txt 文件删除完成！！");
    }



}
